package com.bkhech.home.practice.redisson;

import com.bkhech.home.practice.redisson.codec.FastJsonCodec;
import com.bkhech.home.practice.redisson.domain.BucketDto;
import org.redisson.Redisson;
import org.redisson.api.RBucket;
import org.redisson.api.RList;
import org.redisson.api.RedissonClient;
import org.redisson.client.codec.Codec;
import org.redisson.client.codec.StringCodec;
import org.redisson.codec.JsonCodecWrapper;
import org.redisson.codec.JsonJacksonCodec;
import org.redisson.config.Config;

import java.util.stream.Collectors;

/**
 * Redisson 示例
 *
 * @author guowm
 * @date 2023/9/1
 */
public class Client {

    private static RedissonClient redissonClient = null;
    public static void main(String[] args) {
        initRedissonClient();
        // 1. 基础测试
        basic();
    }

    /**
     * 1. 基础测试
     */
    private static void basic() {
        final RBucket<BucketDto> bucket = redissonClient.getBucket("bucket_test");
        bucket.delete();
        System.out.println(bucket.get());
        bucket.set(BucketDto.builder().name("111").build());
        final BucketDto result = bucket.get();
        System.out.println(result);

        final RList<String> list = redissonClient.getList("list_test");
        list.delete();
        list.add("111");
        list.add("222");
        list.stream().forEach(System.out::println);


    }


    private static void initRedissonClient() {
        // 1. Create config object
        Config config = new Config()
                //自定义序列化方式
                .setCodec(new JsonJacksonCodec());

        config.useSingleServer()
                .setAddress("redis://192.168.85.125:6379")
                .setPassword("123456")
                .setDatabase(12)
        ;

        // Create Redisson instance
        redissonClient = Redisson.create(config);
    }

}
